﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Windows.Interop;

namespace AutoLogOffInWPF
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public int logOffTime = 1;
        DispatcherTimer statusTimer;
        public Window1()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
            this.Closed += new EventHandler(Window1_Closed);
        }

        void Window1_Closed(object sender, EventArgs e)
        {
            Application.Current.Shutdown();
        }
        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            this.statusTimer = new DispatcherTimer();
            this.statusTimer.Interval = TimeSpan.FromSeconds(1);
            this.statusTimer.Start();
            this.statusTimer.Tick += new EventHandler(statusTimer_Tick);
            ReadConfigInfo();
            InitializeAutoLogoffFeature();
        }

        private void ReadConfigInfo()
        {
            try
            {
                string strlogOffTime = System.Configuration.ConfigurationManager.AppSettings["LogOffTime"];
                logOffTime = Convert.ToInt32(strlogOffTime);
                string userName = System.Configuration.ConfigurationManager.AppSettings["UserName"];
                tblLogoffTime.Text = "Logoff Time: " + strlogOffTime;
                tbuserName.Text = "Current User" + ": " + userName;
            }
            catch
            { }
        }

        private void InitializeAutoLogoffFeature()
        {
            HwndSource windowSpecificOSMessageListener = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);
            windowSpecificOSMessageListener.AddHook(new HwndSourceHook(CallBackMethod));
            AutoLogOffHelper.LogOffTime = logOffTime;
            AutoLogOffHelper.MakeAutoLogOffEvent += new AutoLogOffHelper.MakeAutoLogOff(AutoLogOffHelper_MakeAutoLogOffEvent);
            AutoLogOffHelper.StartAutoLogoffOption();
            string time = DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss tt");
            tblStatus.Text = "Timer is started at " + ": " + time;
        }
        private IntPtr CallBackMethod(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            //  Listening OS message to test whether it is a user activity
            if ((msg >= 0x0200 && msg <= 0x020A) || (msg <= 0x0106 && msg >= 0x00A0) || msg == 0x0021)
            {
                AutoLogOffHelper.ResetLogoffTimer();
                string time = DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss tt");
                tblStatus.Text = "Timer is reset on user activity at " + ": " + time;
                tblLastUserActivityTypeOSMessage.Text = "Last user activity type OS message the application considers " + ": 0x" + msg.ToString("X");
            }
            else
            {
              tblLastOSMessage.Text = "Last OS Message " + ":  0x" + msg.ToString("X");
                // For debugging purpose
                // If this auto logoff does not work for some user activity, you can detect the integer code of that activity  using the following line.
                //Then All you need to do is adding this integer code to the above if condition.
                System.Diagnostics.Debug.WriteLine(msg.ToString());
            }
            return IntPtr.Zero;
        }

        void AutoLogOffHelper_MakeAutoLogOffEvent()
        {
            Logoff();
        }

        private void Logoff()
        {
            this.Hide();
            LoginView mainWindow = new LoginView();
            mainWindow.Show();
        }
        void statusTimer_Tick(object sender, EventArgs e)
        {
            this.systemDate.Text = DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss tt");
        }
        private void btnExit_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }
        private void Logoff_Click(object sender, RoutedEventArgs e)
        {
            Logoff();
        }
    }
}
